﻿using Microsoft.AspNetCore.Mvc;
using MiniFox.Net;
using MiniFox.Net.Http;
using MiniFox.Web.Infrastructure;

namespace MiniFox.Platform.Web
{
    /// <summary>
    /// 
    /// </summary>
    [NoCache]
    public class GatewayController : MvcControllerBase
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="adapter"></param>
        /// <param name="service"></param>
        /// <returns></returns>
        protected virtual HttpService CreateHttpService(string adapter, string service)
        {
            var httpAdapter = WebAdapterFactory.Container.GetHttpAdapterByName(adapter);
            var httpService = httpAdapter.GetService(service);
            //IDictionary<string, object> headers = new Dictionary<string, object>();
            //foreach (var header in this.Request.Headers)
            //{
            //    headers.Add(header.Key, header.Value);
            //}
            return httpService;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="adapter"></param>
        /// <param name="service"></param>
        /// <returns></returns>
        [OpenApi]//[Route("gateway/{adapter}/{service}")]
        public async Task<HttpResponseMessage> Get([FromRoute] string adapter, [FromRoute] string service)
        {
            var httpService = CreateHttpService(adapter, service);
            var content = await httpService.GetAsync(this.Request.QueryString, this.Request.Headers);
            await using (var stream = new MemoryStream())
            {
                await content.CopyToAsync(stream);
                stream.Position = 0;
                return new HttpResponseMessage { Content = new StreamContent(stream) };
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="adapter"></param>
        /// <param name="service"></param>
        /// <returns></returns>
        [OpenApi]//[Route("gateway/{adapter}/{service}")]
        public async Task<HttpResponseMessage> Post([FromRoute] string adapter, [FromRoute] string service)
        {
            var httpService = CreateHttpService(adapter, service);
            var content = await httpService.PostAsync(new StreamContent(this.Request.Body), this.Request.QueryString, this.Request.Headers);
            return new HttpResponseMessage { Content = content };
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="adapter"></param>
        /// <param name="service"></param>
        /// <returns></returns>
        [OpenApi]//[Route("gateway/{adapter}/{service}")]
        public async Task<HttpResponseMessage> Put([FromRoute] string adapter, [FromRoute] string service)
        {
            var httpService = CreateHttpService(adapter, service);
            var content = await httpService.PutAsync(new StreamContent(this.Request.Body), this.Request.QueryString, this.Request.Headers);
            return new HttpResponseMessage { Content = content };
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="adapter"></param>
        /// <param name="service"></param>
        /// <returns></returns>
        [OpenApi]//[Route("gateway/{adapter}/{service}")]
        public async Task<HttpResponseMessage> Delete([FromRoute] string adapter, [FromRoute] string service)
        {
            var httpService = CreateHttpService(adapter, service);
            var content = await httpService.DeleteAsync(new StreamContent(this.Request.Body), this.Request.QueryString, this.Request.Headers);
            return new HttpResponseMessage { Content = content };
        }

        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="adapter"></param>
        ///// <param name="service"></param>
        ///// <returns></returns>
        //[OpenApi]//[Route("gateway/{adapter}/{service}")]
        //public async Task<HttpResponseMessage> Head(string adapter, string service)
        //{
        //    var httpService = CreateHttpService(adapter, service);
        //    var response = await httpService.HeadAsync(this.Request.QueryString, this.Request.Headers);
        //    return response;
        //}
    }
}
